/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lwjgl.TrueTypeFont;
import glapp.GLApp;

import java.awt.Font;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Rectangle;

public class GameTextLabel extends UIElement {
	TrueTypeFont ttFont;
	String text;
	float maxFontSize;
	final float FONTSIZE = 24;
	int textY;
	boolean centerHorizontaly;
	boolean isOn = false;
	boolean isDrawFrame = false;
	float fontScale;
	float[] textColor;
	
	public GameTextLabel(Rectangle rectangle, int maxFontSize, float[] textColor) {
		super(rectangle);
		this.textColor = textColor;
		Font font;
		this.maxFontSize = maxFontSize;
		centerHorizontaly = true;
		if(TrueTypeFont.isSupported(LModeller.getGameSetting().getFontName()))
		{
			font = new Font(LModeller.getGameSetting().getFontName(), Font.BOLD, (int)FONTSIZE);
		}
		else
		{
			font = new Font("Arial", Font.BOLD, (int)FONTSIZE);
			System.out.println("Font "+LModeller.getGameSetting().getFontName()+" not supported, using Arial");
		}
		
		ttFont = new TrueTypeFont(font, true);

		setMaxFontSize(maxFontSize);
	}

	public void centerHorizontaly(boolean center) {
		centerHorizontaly = center;
	}
	
	public void toggleOnOff()
	{
		isOn = (isOn)?false:true;
	}
	
	public void setOnOff(boolean on)
	{
		isOn = on;
	}
	
	public void showFrame(boolean isDrawFrame)
	{
		this.isDrawFrame = isDrawFrame;
	}

	public void paint()
	{
		GL11.glPushMatrix();
    	GL11.glEnable(GL11.GL_TEXTURE_2D);
		if(isOn)
		{
        	GLApp.setColor(0.4f, 0.4f, 0.4f, .8f);
    		fillRect(this.getBounds().getX(), 
    				this.getBounds().getY(), this.getBounds().getWidth(), this.getBounds().getHeight());
        	GL11.glEnable(GL11.GL_TEXTURE_2D);
		}
		if(text!=null && text.length()>0)
		{
            GLApp.setColor(textColor);
			TrueTypeFont.set2DMode(0, GLApp.getWidth(), 0, GLApp.getHeight());
	    	if(centerHorizontaly)
	    	{
	    		ttFont.drawString(this.getBounds().getX() + this.getBounds().getWidth()/2, this.getBounds().getY() /*+textY*/, text, 
	    				fontScale, fontScale, TrueTypeFont.ALIGN_CENTER);
	    	}
	    	else
	    	{
	    		ttFont.drawString(this.getBounds().getX()/* + textX */, this.getBounds().getY()/*+textY*/, text, 
	    				fontScale, fontScale, TrueTypeFont.ALIGN_LEFT);
	    	}
			TrueTypeFont.set3DMode();
		}
		if(isDrawFrame)
		{
            GLApp.setColor(textColor);
            GLApp.drawRect(this.getBounds().getX(), 
    				this.getBounds().getY(), this.getBounds().getWidth(), this.getBounds().getHeight());
        	GL11.glEnable(GL11.GL_TEXTURE_2D);
		}
        GLApp.setColor(GLApp.colorWhite);
        GL11.glPopMatrix();
	}
	
	public String getText()
	{
		return this.text;
	}
	
	public int setText(String text)
	{
		fontScale = maxFontSize / FONTSIZE;
		if(text!=null && text.length()>0)
		{
			this.text = text;
			if(this.getBounds().getHeight()<ttFont.getHeight()*fontScale)
			{
				fontScale = this.getBounds().getHeight()/(float)ttFont.getHeight();
			}
			if(this.getBounds().getWidth()<ttFont.getPreciseWidth(text,TrueTypeFont.ALIGN_LEFT)*fontScale)
			{
				fontScale = this.getBounds().getWidth() / (float)ttFont.getPreciseWidth(text,TrueTypeFont.ALIGN_LEFT);
			}
			this.textY = (int) (this.getBounds().getY()+((this.getBounds().getHeight()-ttFont.getHeight()*fontScale))/2);
		}
		else
		{
			this.text = "";
			this.textY = this.getBounds().getY();
		}
		return (int)(FONTSIZE*fontScale);
	}

	
	public int getCurrentFontSize()
	{
		return (int)(FONTSIZE*fontScale);
	}
	
	public int setMaxFontSize(int fontSize)
	{
		if(fontSize<1)
		{
			maxFontSize = 1;
		}
		else
		{
			maxFontSize = fontSize;
		}
		fontScale = maxFontSize / FONTSIZE;
		this.textY = this.getBounds().getY();
		if(text!=null && text.length()>0)
		{
			if(this.getBounds().getHeight()<ttFont.getHeight()*fontScale)
			{
				fontScale = this.getBounds().getHeight()/(float)ttFont.getHeight();
			}
			if(this.getBounds().getWidth()<ttFont.getPreciseWidth(text,TrueTypeFont.ALIGN_LEFT)*fontScale)
			{
				fontScale = this.getBounds().getWidth() /(float) ttFont.getPreciseWidth(text,TrueTypeFont.ALIGN_LEFT);
			}
			this.textY = (int) (this.getBounds().getY()+((this.getBounds().getHeight()-ttFont.getHeight()*fontScale))/2);
		}
		return (int)(FONTSIZE*fontScale);
	}
}
